#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Author:Bruno Alves
data criação: 2012/02/15
versão: v1.0
'''

import SimpleHTTPServer
import SocketServer
import time
import csv
import webbrowser


#Class for HTTPService
class Http:



    def __init__(self):
        pass

   
    def HTTPServer(self):
        PORT = 8000
        Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        httpd = SocketServer.TCPServer(('', PORT), Handler)        
        print "HTTP serving at port", PORT
        httpd.serve_forever()
        


    def HTTPServerDisable(self):
        self.server.kill()
        print "HTTP server stopped"       



    def WebPage(self):
        PORT = 8000
        Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        httpd = SocketServer.TCPServer(('', PORT), Handler)        
        print "HTTP serving at port", PORT

        url = 'http://127.0.0.1:8000/'
        webbrowser.open(url)
        httpd.serve_forever()


    def HTMLFile1(self, file_name, structure):
        
        try:
            print 'Generating HTML, wait a moment please...'
            timei = time.clock()
            ficheiro = file('index.html', 'w')
        
            if structure == 1:
                label = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>\
                        <h4>Teachers by School and Year</h4>\
                        <table border="1" align="center" width="100%">\
                        <tr>\
                        <th>ANO</th><th>School</th><th>Teachers</th>\
                        </tr>'
                ficheiro.write(label)
                pass
           
            elif structure == 2:
                label = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>\
                        <h4>All Teachers by School And Year</h4>\
                        <table border="1" align="center" width="100%">\
                        <tr>\
                        <th>Year</th><th>School</th><th>Teachers</th>\
                        </tr>'
                ficheiro.write(label)
                pass
            spamReader = csv.reader(open(file_name, 'rb'), delimiter=',')
            
            for row in spamReader:
                label ='<tr>\
                        <td>' + str(row[0]) + '</td>\
                        <td>' + str(row[1]) + '</td>\
                        <td>' + str(row[2]) + '</td>\
                        </tr>'
                ficheiro.write(label)
                pass
            ficheiro.write('</table>')
            label_2 = '</body></html>'
            ficheiro.write(label_2)
            ficheiro.close()
            timef = time.clock()
            print 'Time Elasped =', timef-timei
            print 'HTML File Done!.'
            pass
        except:
            print 'File Not Found!!'
            pass        


    def HTMLFile2(self, file_name, structure):
        
        try:
            print 'Generating HTML, wait a moment please...'
            timei = time.clock()
            ficheiro = file('index.html', 'w')
           
            if structure == 1:
                label = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>\
                        <h4>Teachers by Level, School and Year</h4>\
                        <table border="1" align="center" width="100%">\
                        <tr>\
                        <th>Year</th><th>School</th><th>Level</th><th>Teachers</th>\
                        </tr>'
                ficheiro.write(label)
                pass
          
            elif structure == 2:
                label = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>\
                        <h4>All Teachers by Level, School and Year</h4>\
                        <table border="1" align="center" width="100%">\
                        <tr>\
                        <th>Year</th><th>School</th><th>Level</th><th>Teachers</th>\
                        </tr>'
                ficheiro.write(label)
                pass
            spamReader = csv.reader(open(file_name, 'rb'), delimiter=',')
            
            for row in spamReader:
                label ='<tr>\
                        <td>' + str(row[0]) + '</td>\
                        <td>' + str(row[1]) + '</td>\
                        <td>' + str(row[2]) + '</td>\
                        <td>' + str(row[3]) + '</td>\
                        </tr>'
                ficheiro.write(label)
                pass
            ficheiro.write('</table>')
            label_2 = '</body></html>'
            ficheiro.write(label_2)
            ficheiro.close()
            timef = time.clock()
            print 'temp =', timef-timei
            print 'HTML File Done!.'
            pass
        except:
            print 'File Not Found!!'
            pass             




    def HTMLFile3(self, file_name, structure):
        
        try:
            print 'Generating HTML, wait a moment please...'
            timei = time.clock()
            ficheiro = file('index.html', 'w')
           
            if structure == 1:
                label = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>\
                        <h4>Teachers by Highest Level, School and Year</h4>\
                        <table border="1" align="center" width="100%">\
                        <tr>\
                        <th>Year</th><th>School</th><th>ÚLTIMO GRAU</th><th>Teachers</th>\
                        </tr>'
                ficheiro.write(label)
                pass
            
            elif structure == 2:
                label = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>\
                        <h4>Teachers by Highest Level, School and Year</h4>\
                        <table border="1" align="center" width="100%">\
                        <tr>\
                        <th>Year</th><th>School</th><th>Highest Level</th><th>Teachers</th>\
                        </tr>'
                ficheiro.write(label)
                pass
            spamReader = csv.reader(open(file_name, 'rb'), delimiter=',')
           
            for row in spamReader:
                label ='<tr>\
                        <td>' + str(row[0]) + '</td>\
                        <td>' + str(row[1]) + '</td>\
                        <td>' + str(row[2]) + '</td>\
                        <td>' + str(row[3]) + '</td>\
                        </tr>'
                ficheiro.write(label)
                pass
            ficheiro.write('</table>')
            label_2 = '</body></html>'
            ficheiro.write(label_2)
            ficheiro.close()
            timef = time.clock()
            print 'Time Elasped =', timef-timei
            print 'HTML File Done!.'
            pass
        except:
            print 'File Not Found!!'
            pass



    def Graphic1(self, file_name):

        try:
            print 'Generating HTML, wait a moment please...'
            timei = time.clock()
            ficheiro = file('index.html', 'w')
            
            
            label = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>\
                      <h4>Teachers by School And Year (Graphic)</h4>\
                      <table border="1" align="center" width="100%">\
                      <img src="{0}" alt="Graphic" />'.format(file_name)
            ficheiro.write(label)
            label_2 = '</body></html>'
            ficheiro.write(label_2)
            ficheiro.close()
            timef = time.clock()
            print 'Time Elasped =', timef-timei
            print 'HTML File Done!.'

        except:
            print 'Graphic File Not Found!!'
            pass

    def HtmlTags(self, file_name):

        try:
            print 'Generating HTML, wait a moment please...'
            timei = time.clock()
            ficheiro = file('index.html', 'w')
 
            label = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>\
                    <h4>HTML Tags per Web Page</h4>\
                    <table border="1" align="center" width="100%">\
                    <tr>\
                    <th>HtmlTAGS</th><th>How Many</th>\
                    </tr>'
            ficheiro.write(label)

            spamReader = csv.reader(open(file_name, 'rb'), delimiter=',')
            
            for row in spamReader:
                label ='<tr>\
                        <td align="center">' + str(row[0]) + '</td>\
                        <td align="center">' + str(row[1]) + '</td>\
                        </tr>'
                ficheiro.write(label)
                pass
            ficheiro.write('</table>')
            label_2 = '<tr>\
                        <h4>HTML Tags per Web Page (Graphic)</h4>\
                        <img src="Graphic4.png" alt="grafico"/>\
                        </body></html>'
            ficheiro.write(label_2)
            ficheiro.close()
            timef = time.clock()
            print 'Time Elasped =', timef-timei            
            print 'HTML File Done!.'
            pass
        except:
            print 'File Not Found!!'
            pass

